package com.liyunc.demo.config;
/**
 * Copyright (c) 2018-2022 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

import com.liyunc.demo.filter.JwtFilter;
import com.liyunc.demo.filter.JwtLoginFilter;
import com.liyunc.demo.filter.ValidateCodeFilter;
import com.liyunc.demo.handler.MyAuthenticationFailureHandler;
import com.liyunc.demo.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author liyuncong
 * @version 1.0
 * @file
 * @brief
 * @details
 * @date 2022-02-21
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2022-02-21               liyuncong        Created
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
    securedEnabled = true,
    prePostEnabled = true,
    jsr250Enabled = true
)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    MyAuthenticationFailureHandler myAuthenticationFailureHandler;

    @Autowired
    private ValidateCodeFilter validateCodeFilter;

    @Autowired
    private JwtFilter jwtFilter;


    /**
     * 登录配置.
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers(
                "/",
                "/ui/login",
                "/ui/register",
                "/ui/logout",
                "/api/v1/auth/**")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
            .addFilterBefore(
                new JwtLoginFilter("/api/v1/auth/form", authenticationManager()),
                UsernamePasswordAuthenticationFilter.class)
            .addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class)
            .formLogin()
            .usernameParameter("username")
            .passwordParameter("password")
            .loginPage("/ui/login")
            .permitAll()
            .loginProcessingUrl("/api/v1/auth/form")
            .failureHandler(myAuthenticationFailureHandler)
            .permitAll()
            .defaultSuccessUrl("/ui/home")
            .and()
            .logout()
            .logoutUrl("/ui/logout")
            .logoutSuccessUrl("/")
            .permitAll()
            .and()
            .csrf()
            .disable();
    }

    /**
     * 忽略拦截.
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .userDetailsService(userDetailsService())
            .passwordEncoder(encoder());
    }

    /**
     * 注册UserDetailsService的bean.
     */
    @Bean
    public UserDetailsService userDetailsService() {
        return new UserInfoService();
    }

    /**
     * 将加密工具类加入到IOC容器中，便于加密.
     */
    @Bean
    public BCryptPasswordEncoder encoder() {
        return new BCryptPasswordEncoder();
    }

}
